package com.tianxiabuyi.utils;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 后台验证常用输入框工具集
 */
public final class ValidateUtil {

    /**
     * 匹配手机号正则表达式
     */
    private static final Pattern PHONE_PATTERN = Pattern.compile("^((13[0-9])|(14[5|7])|(15[0-9])|(17[3|6-8])|(18[0-9]))\\d{8}$");

    /**
     * 匹配微信号
     */
    private static final Pattern WECHAT_PATTERN = Pattern.compile("^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}+$");

    /**
     * 匹配QQ号
     */
    private static final Pattern QQ_PATTERN = Pattern.compile("^[1-9][0-9]{4,10}$");

    /**
     * 匹配邮箱
     */
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");

    /**
     * 匹配汉字正则表达式
     */
    private static final Pattern CHINESE_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5]$");

    /**
     * 匹配空格正则表达式
     */
    private static final Pattern SPACE_PATTERN = Pattern.compile("^\\s$");

    /**
     * 验证手机号
     * @param phone
     * @return
     */
    public static boolean validatePhone(String phone) {
        return validate(PHONE_PATTERN, phone);
    }

    /**
     * 验证微信号
     * @param wechat
     * @return
     */
    public static boolean validateWechat(String wechat) {
        return validate(WECHAT_PATTERN, wechat);
    }

    /**
     * 验证qq号
     * @param qq
     * @return
     */
    public static boolean validateQQ(String qq) {
        return validate(QQ_PATTERN, qq);
    }

    /**
     * 验证邮箱
     * @param email
     * @return
     */
    public static boolean validateEmail(String email) {
        return validate(EMAIL_PATTERN, email);
    }

    /**
     * 验证是否有中文
     * @param source
     * @return
     */
    public static boolean hasChinese(String source) {
        return hasSpecificChar(CHINESE_PATTERN, source);
    }

    /**
     * 验证是否有空格
     * @param source
     * @return
     */
    public static boolean hasSpace(String source) {
        if (Objects.isNull(source))
            return false;
        Matcher matcher = SPACE_PATTERN.matcher(source);
        return matcher.find();
    }

    /**
     * 验证字符串长度合法
     * @param source
     * @param min 最小长度
     * @param max 最大长度
     * @return
     */
    public static boolean legalLength(String source, int min, int max) {
        if (Objects.isNull(source))
            return false;
        int length = source.length();
        return length >= min && length <= max;
    }

    /**
     * 判断是否为空
     * @param source
     * @return
     */
    public static boolean isEmpty(Object source) {
        source = Tools.getDesc(source, "");
        if ("".equals(source))
            return true;
        return false;
    }

    /**
     * 验证是否有特定字符
     * @param regex: 正则表达式字符串
     * @param source: 待校验字符串
     * @return: 校验结果
     */
    public static boolean hasSpecificChar(String regex, String source) {
        boolean flag = false;
        if (regex == null) {
            return flag;
        }
        Pattern pattern = null;
        try {
            pattern = Pattern.compile(regex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (pattern == null) {
            return flag;
        }
        flag = hasSpecificChar(pattern, source);
        return flag;
    }

    /**
     * 验证是否有特定字符
     * @param pattern
     * @param source
     * @return
     */
    public static boolean hasSpecificChar(Pattern pattern, String source) {
        boolean flag = false;
        if (pattern == null) {
            return flag;
        }
        source = Tools.getDesc(source, "");
        Matcher matcher = pattern.matcher(source);
        flag = matcher.find();
        return flag;
    }

    /**
     * 正则表达式校验字符串
     * @param regex: 正则表达式字符串
     * @param source: 待校验字符串
     * @return: 校验结果
     */
    public static boolean validate(String regex, String source) {
        boolean flag = false;
        if (regex == null) {
            return flag;
        }
        Pattern pattern = null;
        try {
            pattern = Pattern.compile(regex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (pattern == null) {
            return flag;
        }
        flag = validate(pattern, source);
        return flag;
    }

    /**
     * Pattern对象校验字符串
     * @param pattern: Pattern对象
     * @param source: 待校验字符串
     * @return: 校验结果
     */
    public static boolean validate(Pattern pattern, String source) {
        boolean flag = false;
        if (pattern == null) {
            return flag;
        }
        source = Tools.getDesc(source, "");
        Matcher matcher = pattern.matcher(source);
        flag = matcher.matches();
        return flag;
    }

}
